﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Class : System.Web.UI.Page
    {
        private string[] parasValue = new string[]{};
        protected void Page_Load(object sender, EventArgs e)
        {
         
            if (!Page.IsPostBack)
            {
                parasValue = new string[]{txtUpClassName.Text.Trim(),ddlClassSub.Text,ddlClassUser.Text
               , ddlClassDep.Text, ClassFlag().ToString()};
                int page = new BLL.Class().MaxPage(parasValue);
               
                Session["classId"] = null;
                Session["page"] = 1;
                Session["maxPage"] = page;
                //Session["getFlag"] = "Show";
                //ShowData(new BLL.Class().Formation(),int.Parse(Session["maxPage"].ToString()));
                ShowData(new BLL.Class().Search(parasValue), int.Parse(Session["maxPage"].ToString()));
                Session["SearchArr"] = parasValue;

                DataTable dtDDLClassUser = new BLL.User().GetUserName();
                ddlClassUser.Items.Clear();
                ddlAlterClassTea.DataSource = ddlAddClassTea.DataSource =ddlClassUser.DataSource = dtDDLClassUser;
                ddlAlterClassTea.DataTextField = ddlAddClassTea.DataTextField = ddlClassUser.DataTextField = "user_name";
                ddlAlterClassTea.DataValueField = ddlAddClassTea.DataValueField = ddlClassUser.DataValueField = "user_id";
                ddlClassUser.DataBind();
                ddlClassUser.Items.Insert(0, new ListItem("", ""));
                ddlAddClassTea.DataBind();
                ddlAlterClassTea.DataBind();

                DataTable dtDDLSub = new BLL.Sub().GetSubName();
                ddlClassSub.Items.Clear();
                ddlAlterClassSub.DataSource = ddlAddClassSub.DataSource = ddlClassSub.DataSource = dtDDLSub;
                ddlAlterClassSub.DataTextField = ddlAddClassSub.DataTextField = ddlClassSub.DataTextField = "sub_name";
                ddlAlterClassSub.DataValueField = ddlAddClassSub.DataValueField = ddlClassSub.DataValueField = "sub_id";
                ddlClassSub.DataBind();
                ddlClassSub.Items.Insert(0, new ListItem("", ""));
                ddlAddClassSub.DataBind();
                ddlAlterClassSub.DataBind();

                DataTable dtDDLDep = new BLL.Department().GetDepName();
                ddlClassDep.Items.Clear();
                ddlAlterClassDep.DataSource = ddlAddClassDep.DataSource = ddlClassDep.DataSource = dtDDLDep;
                ddlAlterClassDep.DataTextField =  ddlAddClassDep.DataTextField = ddlClassDep.DataTextField = "dep_name";
                ddlAlterClassDep.DataValueField = ddlAddClassDep.DataValueField = ddlClassDep.DataValueField = "dep_id";
                ddlClassDep.DataBind();
                ddlClassDep.Items.Insert(0, new ListItem("", ""));
                ddlAddClassDep.DataBind();
                ddlAlterClassDep.DataBind();
                



            }
            
        }


        /// <summary>
        /// 增加班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            


            MODEL.Class cs = new MODEL.Class();
            cs.ClassName = txtTempClassName.Text;
            cs.ClassTeacherId = int.Parse(txtTempClassTea.Text);
            cs.ClassSubId = int.Parse(txtTempClassSub.Text);
            cs.ClassDepId = int.Parse(txtTempClassDep.Text);
            int result = new BLL.Class().Add(cs);
            if (result == 1)
            {
                parasValue = (string[])Session["SearchArr"];
                Session["maxPage"] = new BLL.Class().MaxPage((string[])Session["SearchArr"]);
                ShowData(new BLL.Class().Search(parasValue),int.Parse(Session["maxPage"].ToString()));
            }

        }
       
        /// <summary>
        /// 查找符合条件的班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
           // XHCTMS.MODEL.Class cs = new MODEL.Class();
            //if (!string.IsNullOrEmpty(txtClassName.Value))
            //{
            //    cs.ClassName = this.txtClassName.Value;
            //}
            //if (!string.IsNullOrEmpty(ddlClassUser.Text))
            //{
            //    cs.ClassTeacherId = Convert.ToInt32(this.ddlClassUser.Text);
            //}
            //if (!string.IsNullOrEmpty(ddlClassSub.Text))
            //{
            //    cs.ClassSubId = Convert.ToInt32(this.ddlClassSub.Text);
            //}
            //if (!string.IsNullOrEmpty(ddlClassSub.Text))
            //{
            //    cs.ClassSubId = Convert.ToInt32(this.ddlClassSub.Text);
            //}
       
            parasValue = new string[]{txtUpClassName.Text.Trim(),ddlClassSub.SelectedItem.Text,ddlClassUser.SelectedItem.Text
                , ddlClassDep.SelectedItem.Text, ClassFlag().ToString()};
            Session["SearchArr"] = parasValue;

            //DataTable dt = new BLL.Class().Search(parasValue);
            //this.infolist.DataSource = dt;
            //this.infolist.DataBind();
            Session["page"] = 1;
            Session["maxPage"] = new BLL.Class().MaxPage(parasValue);

            ShowData(new BLL.Class().Search(parasValue), int.Parse(Session["maxPage"].ToString()));
          
        }

        /// <summary>
        /// 修改某班级信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAlter_Click(object sender, EventArgs e)
        {
            // int classFlag = 1;

            //if (rbtnSchool.Checked == true)
            //{
            //    classFlag = 1;
            //}
            //else
            //{
            //    if (rbtnGraducat.Checked == true)
            //    {
            //        classFlag = 0;
            //    }
            //}
            //MODEL.Class cl = new MODEL.Class();
            //cl.ClassName = this.txtUpClassName.Text.Trim();
            //cl.ClassDepId = int.Parse(this.ddlClassDep.Text);
            //cl.ClassSubId = int.Parse(this.ddlClassSub.Text);
            //cl.ClassTeacherId = int.Parse(this.ddlClassUser.Text);
            //cl.ClassFlag = byte.Parse(classFlag.ToString());
            //string classId = this.txtTempClassID.Text;
            //int result = new BLL.Class().Update(int.Parse(classId),cl);
            //ShowData();
    
            MODEL.Class cl = new MODEL.Class();
            cl.ClassName = txtTempClassName.Text.Trim();
            int classID = int.Parse(txtTempClassID.Text.Trim());
            cl.ClassDepId = int.Parse(txtTempClassDep.Text);
            cl.ClassSubId = int.Parse(txtTempClassSub.Text);
            cl.ClassTeacherId = int.Parse(txtTempClassTea.Text);
            if (new BLL.Class().GetClassFlag(classID) == "True")
            {
                cl.ClassFlag = 1;
            }
            else
            {
                cl.ClassFlag = 0;
            }
            int result = new BLL.Class().Update(int.Parse(txtTempClassID.Text.Trim()), cl);
            if (result == 1)
            {
                parasValue = (string[])Session["SearchArr"];
                ShowData(new BLL.Class().Search(parasValue,int.Parse(Session["page"].ToString())),int.Parse(Session["maxPage"].ToString()));
            }
            
            this.txtTempClassID.Text = "";
           
        }





        protected void infolist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int classId = Convert.ToInt32(e.CommandArgument.ToString());
            Session["classId"] = classId;
            switch (e.CommandName)
            {
                case "linkbtnRevise":
                    
                   
                    break;
                case "linkbtnDelete":
                    int result = new BLL.Class().Delete(classId);
                    ShowData();
                    break;
            }           
        }

        private void ShowData()
        {
            XHCTMS.BLL.Class Class = new BLL.Class();
            this.infolist.DataSource = Class.Formation();
            this.infolist.DataBind();
        }

        /// <summary>
        /// 更新infolist的信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="maxPage"></param>
        private void ShowData(DataTable dt, int maxPage)
        {
            List<string> pageList = new List<string> { };
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString() + "/" + maxPage.ToString());
            }
            this.ddlPage.DataSource = pageList;
            this.ddlPage.DataBind();
            this.ddlPage.SelectedIndex = int.Parse(Session["page"].ToString()) - 1;
            this.infolist.DataSource = dt;
                
            this.infolist.DataBind();
        }

        /// <summary>
        /// 班级毕业
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGraduation_Click(object sender, EventArgs e)
        {
            string classId = this.txtTempClassID.Text;
            int result;
            try
            {
                result = new BLL.Class().Delete(int.Parse(classId));
            }
            catch
            {
                result = 0;
            }
            this.txtTempClassID.Text = "";
            if (result > 0)
            {
                parasValue = (string[])Session["SearchArr"];
                Session["maxPage"] = new BLL.Class().MaxPage(parasValue);
                ShowData(new BLL.Class().Search(parasValue), int.Parse(Session["maxPage"].ToString()));
            }
          
        }

        private void ClearControl()
        {
            this.txtUpClassName.Text = "";
            this.ddlClassDep.SelectedIndex = 0;
            this.ddlClassSub.SelectedIndex = 0;
            this.ddlClassUser.SelectedIndex = 0;
            this.rbtnSchool.Checked = true;
        }





        private int ClassFlag()
        {
            int clasFlag = 1;

            if (rbtnSchool.Checked == true)
            {
                clasFlag = 1;
            }
            else
            {
                if (rbtnGraducat.Checked == true)
                {
                    clasFlag = 0;
                }
            }
            return clasFlag;
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {    

            parasValue = (string[])Session["SearchArr"];
            int count = int.Parse(Session["page"].ToString()) - 1;
            if (count > 0)
            {
                ShowData(new BLL.Class().Search(parasValue, count), int.Parse(Session["maxPage"].ToString()));
                Session["page"] = count;
                this.ddlPage.SelectedIndex = count - 1;
            }

        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];                    
            ShowData(new BLL.Class().Search(parasValue, 1), int.Parse(Session["maxPage"].ToString()));
            Session["page"] = 1;
            this.ddlPage.SelectedIndex = 0;         
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int maxPage = int.Parse(Session["maxPage"].ToString());
            ShowData(new BLL.Class().Search(parasValue, maxPage), maxPage);
            Session["page"] = maxPage;
            this.ddlPage.SelectedIndex = maxPage -1;   
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(Session["page"].ToString()) + 1;
            ShowData(new BLL.Class().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
            Session["page"] = page;
            this.ddlPage.SelectedIndex = page - 1;
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            ShowData(new BLL.Class().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
            Session["page"] = page;
            this.ddlPage.SelectedIndex = page - 1;
        }
    }
}